<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/04/16
 * Time: 14:12
 */

namespace app\api\model;

use think\Controller;
use think\Model;
use think\Db; 

class Toexcel extends Model
{
    
    public function daochu($settlement_id){  
         $topNumber = 2;
        $settlementInfo = Db::name('settlements')->where('settlement_id',$settlement_id)->find();
        if(empty($settlementInfo))return false; 
        if($settlementInfo['object_type'] == 1){ 
            $xlsData = Db::name('hotel_order')
            ->field("order_id,order_sn,pay_type,total_price,order_price,commission_fee,create_time") 
            ->where("settlement_id",$settlement_id)
            ->select();  
            $hotel_name = Db::name('hotel')->where("hotel_id",$settlementInfo['object_id'])->value("hotel_name");
        }elseif($settlementInfo['object_type'] == 2){ 
             $xlsData =Db::name('tour_order') 
            ->field("order_id,order_sn,pay_type,ticket_price as total_price,order_price,commission_fee,create_time") 
            ->where("settlement_id",$settlement_id)
            ->select(); 
            $hotel_name = Db::name('tours')->where("tour_id",$settlementInfo['object_id'])->value("tour_name"); 
        }elseif($settlementInfo['object_type'] == 3){
             $xlsData = Db::name('gtour_order') 
            ->field("order_id,order_sn,pay_type,total_price,order_price,commission_fee,create_time") 
            ->where("settlement_id",$settlement_id)
            ->select();  
            $hotel_name = Db::name('team')->where("team_id",$settlementInfo['object_id'])->value("team_name");
        } 
        Vendor('PHPExcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
        Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
        Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
        $objExcel = new \PHPExcel(); 
        //set document Property
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007'); 
        $objActSheet = $objExcel->getActiveSheet(); 
        $outfiles =   $hotel_name.date('Ymd',time())."已结算订单信息列表";
        $outfile = $outfiles.".xlsx";  
       //垂直居中 
        $objExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

         //处理表头标题
        $objExcel->getActiveSheet()->mergeCells('A1:G1');//合并单元格（如果要拆分单元格是需要先合并再拆分的，否则程序会报错）
        $objExcel->setActiveSheetIndex(0)->setCellValue('A1',$outfiles);
        $objExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $objExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);  
        $objExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
 



        $key = ord("A");
        $letter =explode(',',"A,B,C,D,E,F,G,H,I,J");
        $arrHeader = array('订单id','订单编号','支付方式','商品金额','订单总额','需结算金额','下单时间');
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]".$topNumber,"$arrHeader[$i]");
        };
        $start = $topNumber+1;
        $j = $topNumber+1;
        //填充表格信息
        foreach($xlsData as $k=>$v){
            $k +=3;
            $objActSheet->setCellValue('A'.$k, $v['order_id']);
            $objActSheet->setCellValue('B'.$k, ' '.$v['order_sn']);
            $objActSheet->setCellValue('C'.$k, lvPayType($v['pay_type']));
            $objActSheet->setCellValue('D'.$k, $v['order_price']);
            $objActSheet->setCellValue('E'.$k, $v['total_price']); 
            $objActSheet->setCellValue('F'.$k, $v['commission_fee']);
            $objActSheet->setCellValue('G'.$k, date('Y-m-d H:i:s',$v['create_time'])); 
            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(20);
        }

        
        $width = array(10,15,20,25,30);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[1]);
        $objActSheet->getColumnDimension('B')->setWidth($width[4]);
        $objActSheet->getColumnDimension('C')->setWidth($width[2]);
        $objActSheet->getColumnDimension('D')->setWidth($width[2]);
        $objActSheet->getColumnDimension('E')->setWidth($width[1]);
        $objActSheet->getColumnDimension('F')->setWidth($width[1]);
        $objActSheet->getColumnDimension('G')->setWidth($width[4]);
        $objActSheet->getColumnDimension('H')->setWidth($width[1]);
        $objActSheet->getColumnDimension('I')->setWidth($width[1]);  
        //ob_end_clean();  
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download"); 
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: GBK");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache"); 
        $file_path = ROOT_PATH.'public/settlementexcel/'.$outfile;
        $objWriter->save($file_path);
        return 'http://'.$_SERVER['SERVER_NAME'].'/settlementexcel/'.$outfile;
    }
}